package sea.plot;

import java.util.ArrayList;
import sea.util.PublicParam;
import sea.util.SimHyAHelper;
import sea.util.WinHelper;


/**
 *
 * This class builds the layout of Hist
 *
 * @author  Zhirun Ding
 * 
 * Created on 05/07/2012
 * 
 */

public class PlotHistForm extends javax.swing.JDialog {
    
    /** Creates new form PlotHistForm */
    public PlotHistForm(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        simulationvarsList = new javax.swing.JList();
        selectallButton = new javax.swing.JButton();
        deselectButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        finalStateRadioButton = new javax.swing.JRadioButton();
        timeRadioButton = new javax.swing.JRadioButton();
        givenTimeText = new javax.swing.JTextField();
        binsCheckBox = new javax.swing.JCheckBox();
        binsText = new javax.swing.JTextField();
        backButton = new javax.swing.JButton();
        confirmButton = new javax.swing.JButton();
        helpButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(sea.SimHyAGui.class).getContext().getResourceMap(PlotHistForm.class);
        setTitle(resourceMap.getString("PlotHistForm.title")); // NOI18N
        setName("PlotHistForm"); // NOI18N
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        simulationvarsList.setName("simulationvarsList"); // NOI18N
        jScrollPane1.setViewportView(simulationvarsList);

        selectallButton.setText(resourceMap.getString("selectallButton.text")); // NOI18N
        selectallButton.setName("selectallButton"); // NOI18N
        selectallButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectallButtonActionPerformed(evt);
            }
        });

        deselectButton.setText(resourceMap.getString("deselectButton.text")); // NOI18N
        deselectButton.setName("deselectButton"); // NOI18N
        deselectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deselectButtonActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        finalStateRadioButton.setSelected(true);
        finalStateRadioButton.setText(resourceMap.getString("finalStateRadioButton.text")); // NOI18N
        finalStateRadioButton.setName("finalStateRadioButton"); // NOI18N
        finalStateRadioButton.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                finalStateRadioButtonItemStateChanged(evt);
            }
        });

        timeRadioButton.setText(resourceMap.getString("timeRadioButton.text")); // NOI18N
        timeRadioButton.setName("timeRadioButton"); // NOI18N
        timeRadioButton.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                timeRadioButtonItemStateChanged(evt);
            }
        });

        givenTimeText.setEnabled(false);
        givenTimeText.setName("givenTimeText"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(timeRadioButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(givenTimeText, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(finalStateRadioButton))))
                .addContainerGap(42, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(finalStateRadioButton)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(timeRadioButton)
                    .addComponent(givenTimeText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(11, Short.MAX_VALUE))
        );

        binsCheckBox.setText(resourceMap.getString("binsCheckBox.text")); // NOI18N
        binsCheckBox.setName("binsCheckBox"); // NOI18N
        binsCheckBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                binsCheckBoxItemStateChanged(evt);
            }
        });

        binsText.setText(resourceMap.getString("binsText.text")); // NOI18N
        binsText.setEnabled(false);
        binsText.setName("binsText"); // NOI18N

        backButton.setText(resourceMap.getString("backButton.text")); // NOI18N
        backButton.setName("backButton"); // NOI18N
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });

        confirmButton.setText(resourceMap.getString("confirmButton.text")); // NOI18N
        confirmButton.setName("confirmButton"); // NOI18N
        confirmButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                confirmButtonActionPerformed(evt);
            }
        });

        helpButton.setText(resourceMap.getString("helpButton.text")); // NOI18N
        helpButton.setName("helpButton"); // NOI18N
        helpButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                helpButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(deselectButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(selectallButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(55, 55, 55)
                        .addComponent(backButton, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(confirmButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(helpButton))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(binsCheckBox)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(binsText, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(selectallButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(35, 35, 35)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(binsCheckBox)
                            .addComponent(binsText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(deselectButton)
                    .addComponent(confirmButton)
                    .addComponent(backButton)
                    .addComponent(helpButton))
                .addGap(24, 24, 24))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void confirmButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_confirmButtonActionPerformed
        //generate plot command expression
        toHistCommand();        
        //execute command
        this.setVisible(false);
        SimHyAHelper.executeCommand();        
        //close this form
        this.dispose();        
        //clear all form of plot
        WinHelper.closeFormAfterCommand();
    }//GEN-LAST:event_confirmButtonActionPerformed

    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backButtonActionPerformed
        this.setVisible(false);
        
        if (PublicParam.PreviousForm != null) {
            PublicParam.PreviousForm.setVisible(true);
        }   
    }//GEN-LAST:event_backButtonActionPerformed

    private void selectallButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectallButtonActionPerformed
        //select all vars
        WinHelper.setListSelectall(simulationvarsList, "");
    }//GEN-LAST:event_selectallButtonActionPerformed

    private void deselectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deselectButtonActionPerformed
        int[] selectedIndexes = new int[] {-1};
        simulationvarsList.setSelectedIndices(selectedIndexes); 
    }//GEN-LAST:event_deselectButtonActionPerformed

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        if (PublicParam.PreviousForm != null) {
            PublicParam.PreviousForm.setVisible(false);
        }
        
        //get data variable
        String datavar = ((PlotForm)PublicParam.PlotForm).getCurrentDataVariable();
        if (lastDataVariable.equals(datavar)) return;//no change
        lastDataVariable = datavar;
        
        //get parameter by data variable
        ArrayList<simhya.script.Symbol> vars = PublicParam.SimHyACommand.getVariables(datavar);
        
        //1.init vars list
        WinHelper.initListbox(simulationvarsList, vars);
        //select all vars
        WinHelper.setListSelectall(simulationvarsList, "time");
        
        if (PublicParam.SimHyACommand.exploratorVariableContainsFinalStateData(datavar)
                && !PublicParam.SimHyACommand.exploratorVariableContainsTrajectoryData(datavar)
                || (PublicParam.SimHyACommand.isTrajectoryDataVariable(datavar)
                && PublicParam.SimHyACommand.trajectoryVariableContainsFinalStateData(datavar))) {
            timeRadioButton.setEnabled(false);
            givenTimeText.setEditable(false);
        } else {
            timeRadioButton.setEnabled(true);
            givenTimeText.setEditable(true);
        }
    }//GEN-LAST:event_formComponentShown

    private void helpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpButtonActionPerformed
        WinHelper.showHelp("plot/hist");
    }//GEN-LAST:event_helpButtonActionPerformed

    private void finalStateRadioButtonItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_finalStateRadioButtonItemStateChanged
        givenTimeText.setEnabled(!finalStateRadioButton.isSelected());
    }//GEN-LAST:event_finalStateRadioButtonItemStateChanged

    private void timeRadioButtonItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_timeRadioButtonItemStateChanged
        givenTimeText.setEnabled(timeRadioButton.isSelected());
    }//GEN-LAST:event_timeRadioButtonItemStateChanged

    private void binsCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_binsCheckBoxItemStateChanged
        binsText.setEnabled(binsCheckBox.isSelected());
    }//GEN-LAST:event_binsCheckBoxItemStateChanged
    
    void toHistCommand() {
        String s = "";
        // depends on the selection
        // vars
        if (simulationvarsList.getSelectedValues().length > 0) {
            s += ",vars=[";
            for (int i = 0; i < simulationvarsList.getSelectedValues().length; i++) {
                if (i > 0) s += ",";
                s += simulationvarsList.getSelectedValues()[i].toString();
            }
            s += "]";
        }
        
        //time
        if (finalStateRadioButton.isSelected()) {
            s += ",time=final";
        } else if (timeRadioButton.isSelected() && givenTimeText.getText().length() > 0) {
            s += ",time=" + givenTimeText.getText();
        }        
        
        //bins
        if (binsCheckBox.isSelected() && binsText.getText().length() > 0) {
            s += ",bins=" + binsText.getText();
        }
        
        PublicParam.SimHyACommandExpression += s + ")";
        PublicParam.CommandTypeName = "Hist";        
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                PlotHistForm dialog = new PlotHistForm(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton backButton;
    private javax.swing.JCheckBox binsCheckBox;
    private javax.swing.JTextField binsText;
    private javax.swing.JButton confirmButton;
    private javax.swing.JButton deselectButton;
    private javax.swing.JRadioButton finalStateRadioButton;
    private javax.swing.JTextField givenTimeText;
    private javax.swing.JButton helpButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton selectallButton;
    private javax.swing.JList simulationvarsList;
    private javax.swing.JRadioButton timeRadioButton;
    // End of variables declaration//GEN-END:variables
    private String lastDataVariable = "";
}
